package com.bjpowernode.crm.settings.web.controller;
import com.bjpowernode.crm.settings.domain.DictionaryType;
import com.bjpowernode.crm.settings.domain.DictionaryValue;
import com.bjpowernode.crm.settings.service.DicService;
import com.bjpowernode.crm.utile.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ClassName:DictionaryController
 * package:com.bjpowernode.crm.settings.web.controller
 * Description:
 *
 * @date: 2022-05-27 17:48
 * @author：lin
 */

@Controller
@RequestMapping("/settings/dictionary")
public class DictionaryController {

    @Autowired
    private DicService dicService;





    @RequestMapping("/toIndex.do")
    public String toIndex(){
    //查询字典类型






        return "/settings/dictionary/index";
    }

    //数据字典类型
    @RequestMapping("/type/toTypeIndex.do")
    public  ModelAndView toTypeIndex(){

        List<DictionaryType> type =dicService.getType();

        ModelAndView modelAndView =new ModelAndView();

        modelAndView.addObject("type",type);
        modelAndView.setViewName("/settings/dictionary/type/index");

        return modelAndView;


    }
    //settings/dictionary/type/toSave.do

    //数据字典类型
    @RequestMapping("/type/toSave.do")
    public String toSave(){
        return "/settings/dictionary/type/save";
    }


    //数据字典类型值id验证  id是否重复

    //"settings/dictionary/type/check.do"
    @RequestMapping("/type/check.do")
    @ResponseBody
    public Map<String,Object> check(String code){

        boolean flag =dicService.getTypeCode(code);
        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);

        return map;
    }


    //保存数据字典类型值
    //<form class="form-horizontal" role="form" id="dicTypeForm" action="/settings/dictionary/type/saveType.do">
    @RequestMapping("type/saveType.do")
    public String saveType(DictionaryType type){

        dicService.saveType(type);



        return "redirect:/settings/dictionary/type/toTypeIndex.do";
    }



    //数据字典类型
    @RequestMapping("/type/toEdit.do")
    public ModelAndView toEdit(String code){

        DictionaryType dictionaryType = dicService.toEdit(code);
        ModelAndView modelAndView= new ModelAndView();
        modelAndView.addObject("dictionaryType",dictionaryType);
        modelAndView.setViewName("/settings/dictionary/type/edit");

        return modelAndView;
    }

    //	<form class="form-horizontal" method="post" role="form" id="dicTypeForm" action="settings/dictionary/type/updateType.do">
    //修改数据字典类型
    @RequestMapping("/type/updateType.do")
    public String updateType(DictionaryType dictionaryType){

        dicService.updateType(dictionaryType);




        return "redirect:/settings/dictionary/type/toTypeIndex.do";
    }

    //删除数据字典类型数据
    //window.location.href="settings/dictionary/type/deleteType.do?"+flag;
    @RequestMapping("/type/deleteType.do")
    public  String deleteType(String[] codes){

        dicService.deleteType(codes);

        return "redirect:/settings/dictionary/type/toTypeIndex.do";
    }





    //跳转数据字典值页面
    //settings/dictionary/value/toValueIndex.do

    @RequestMapping("value/toValueIndex.do")
    public ModelAndView toValueIndex(){
        //查询数据字典值
       List<DictionaryValue> DicValue = dicService.selectValue();

        ModelAndView mav = new ModelAndView();
        mav.addObject("DicValue",DicValue);
        mav.setViewName("/settings/dictionary/value/index");


        return mav;

    }

    //数据字典类型
    //settings/dictionary/value/toValueSave.do
    @RequestMapping("/value/toValueSave.do")
    public ModelAndView toValueSave(){
        List<DictionaryType> DicType = dicService.getType();

        ModelAndView mav = new ModelAndView();
        mav.addObject("DicType",DicType);
        mav.setViewName("/settings/dictionary/value/save");


        return mav;



    }


    //"settings/dictionary/type/saveValue.do"
    //保存数据字典值操作
    @RequestMapping("/type/saveValue.do")
    public String saveValue(DictionaryValue dictionaryValue){

         String uuid = UUIDUtil.getUUID();

         dictionaryValue.setId(uuid);
        dicService.saveValue(dictionaryValue);



       return  "redirect:/settings/dictionary/value/toValueIndex.do";

    }




    //数据字典值类型
    //window.location.href="settings/dictionary/value/toValueEdit.do?code="+code;
    @RequestMapping("/value/toValueEdit.do")
    public ModelAndView toValueEdit(String code){
       DictionaryValue dicValue=dicService.selectValueId(code);
       ModelAndView mav =new ModelAndView();
       mav.addObject("dicValue",dicValue);
       mav.setViewName("/settings/dictionary/value/edit");


        return mav;
    }


    //修改字典值数据
    //action="settings/dictionary/value/updateValue.do"
    @RequestMapping("/value/updateValue.do")
    public String updateValue(DictionaryValue value){
        dicService.updateValue(value);


        return  "redirect:/settings/dictionary/value/toValueIndex.do";

    }

    //删除数据字典值数据
    //window.location.href=window.location.href="settings/dictionary/value/deleteValue.do?"+flag;
    @RequestMapping("/value/deleteValue.do")
    public String deleteValue(String[] ids){

        dicService.deleteValue(ids);

        return "redirect:/settings/dictionary/value/toValueIndex.do";
    }











}
